   1 #!/usr/bin/python
   2 # L.S. Keijser - Stone-IT

   3 # keijser@stone-it.com

   4 #

   5 # Will check cobbler server for system and generate Nagios config files

   6 #

   7 # Usage: ./nagios.py -y system_name -k customer_name

   8 # Generated files will then be placed in /etc/nagios/objects/<customer_name>/<system_name>.cfg

   9 #--------------------------------------------------------------------------------------------------------------

  10 import xmlrpclib

  11 from optparse import OptionParser

  12 import re, os, sys 

  13 

  14 # Define login credentials for Cobbler here:

  15 cobbler_uri = "http://11.22.33.44/cobbler_api_rw"

  16 

  17 # Define profile-related services. Be sure to include the same profile for critical and noncritical!

  18 # Define critical profile-related services here (checkname or proc:<name> or tcp:<port> or procargs:<args>)):

  19 services_profile_critical = { 

  20     'default': ['ssh','load','procargs:funcd','procargs:puppetd'], 

  21     'cobbler': ['procargs:cobblerd', 'procargs:puppetmaster'], 

  22     'firewall': [], 

  23     'nas': ['procargs:drbd0_worker', 'procargs:drbd0_receiver', 'procargs:drbd0_asender', 'procargs:drbd1_worker', 'procargs:drbd1_receiver', 'procargs:drbd1_asender', 'proc:heartbeat'], 

  24     'openvpn': ['proc:openvpn'],

  25     'testvirtual': [],

  26     'xenhost': ['procargs:xend'], 

  27     'xenhost-mgmt': ['procargs:xend'] 

  28 }

  29 

  30 # Define noncritical profile-related services here (checkname or proc:<name> or tcp:<port> or procargs:<args>):

  31 services_profile_noncritical = { 

  32     'default': ['ping','users'], 

  33     'cobbler': [], 

  34     'firewall': [], 

  35     'nas': [ 'dell_phydisk', 'dell_power', 'dell_temp', 'dell_virtdisk' ], 

  36     'openvpn': [],

  37     'testvirtual': [],

  38     'xenhost': [ 'dell_phydisk', 'dell_power', 'dell_temp', 'dell_virtdisk' ], 

  39     'xenhost-mgmt': [ 'dell_phydisk', 'dell_power', 'dell_temp', 'dell_virtdisk' ] 

  40 }

  41 

  42 # Define service commands here

  43 checkcommand = {

  44     "ssh": "check_ssh", 

  45     "load": "check_nrpe!check_load!15,10,5 30,25,20", 

  46     "ping": "check_ping!100.0,20%!500.0,60%", 

  47     "users": "check_nrpe!check_users!5 10", 

  48     "proc:": "check_nrpe!check_proc!1", 

  49     "procargs:": "check_nrpe!check_procargs!1", 

  50     "tcp:": "check_tcp!", 

  51     "smtp": "check_smtp",

  52     "http": "check_http",

  53     "yum": "check_nrpe!check_yum",

  54     "kernel": "check_nrpe!check_kernel",

  55     "dell_phydisk": "check_nrpe!check_dell!phydisk",

  56     "dell_power": "check_nrpe!check_dell!power",

  57     "dell_temp": "check_nrpe!check_dell!temp",

  58     "dell_virtdisk": "check_nrpe!check_dell!virtdisk"

  59 }

  60 

  61 ############################################################################

  62 remote = xmlrpclib.Server(cobbler_uri)

  63 

  64 # command line options

  65 parser = OptionParser(usage="%prog -y <system_name> -k <customer_name>",version="%prog 0.0.1")

  66 parser.add_option("-y", "--system", action="store", type="string", dest="system_name", help="Sytem name")

  67 parser.add_option("-k", "--customer", action="store", type="string", dest="customer_name", help="Customer name - One word, no funny business!")

  68 

  69 # parse cmd line options

  70 (options, args) = parser.parse_args()

  71 

  72 # check for all args

  73 if options.system_name is None or options.customer_name is None:

  74     parser.error("Incorrect number of arguments!")

  75 

  76 # check if customer dir exists, create it if it doesn't

  77 customer_dir = '/etc/nagios/objects/' + str(options.customer_name)

  78 

  79 if not os.path.exists(customer_dir):

  80     print "Customer directory didn't exist, making ..."

  81     os.mkdir(customer_dir)

  82 

  83 my_system = remote.get_system(options.system_name)

  84 

  85 print "Now processing system " + options.system_name

  86 

  87 try:

  88     kickstart = remote.generate_kickstart(my_system['profile'], my_system['name'])

  89 except KeyError:

  90     print "Error, profile or system name not found. Bailing out..."

  91     sys.exit()

  92 

  93 # Write kickstart to file

  94 f=open('/tmp/rend-ks.tmp', 'w')

  95 f.write(kickstart)

  96 f.close()

  97 

  98 # Read lines in variable

  99 file=open('/tmp/rend-ks.tmp', 'r')

 100 lines = file.readlines()

 101 file.close()

 102 

 103 # Find line numbers for partition info:

 104 p_begin = re.compile('# BEGINPARTITIONS')

 105 p_end = re.compile('# ENDPARTITIONS')

 106 for i, line in enumerate(lines):

 107     if p_begin.search(line):

 108         beginpart = i + 1

 109     if p_end.search(line):

 110         endpart = i - 1

 111 

 112 p_lines = range(beginpart, endpart)

 113 

 114 fstype = re.compile('fstype')

 115 

 116 partitions = []

 117 

 118 for i, line in enumerate(lines):

 119     if i in p_lines:

 120         if fstype.search(line):

 121             partitions.append(line.split(' ')[1])

 122 

 123 print "\nGenerating Nagios config file for this system in " + str(customer_dir) + "/" + str(my_system['name']) + ".cfg\n"

 124 

 125 f=open(customer_dir + '/' + str(my_system['name']) + '.cfg', 'w')

 126 

 127 # Define host once

 128 f.write("define host{\n")

 129 f.write("\tuse\t\tregular-host\n")

 130 f.write("\thost_name\t" + str(my_system['name']) + "\n")

 131 f.write("\talias\t\t" + str(my_system['name']) + "\n")

 132 f.write("\taddress\t\t" + str(my_system['interfaces']['intf0']['ip_address']) + "\n")

 133 f.write("\t}\n\n")

 134 

 135 # Define all services

 136 # Profile-related Critical services:

 137 for service in services_profile_critical[str(my_system['profile'])]:

 138     f.write("define service{\n")

 139     f.write("\thost_name\t\t" + str(my_system['name']) + "\n")

 140     serv_proc = re.compile('proc:')

 141     serv_tcp = re.compile('tcp:')

 142     serv_procargs = re.compile('procargs:')

 143     if serv_proc.search(service):

 144         f.write("\tservice_description\tProcess " + str(service).split(':')[1] + "\n")

 145         f.write("\tcheck_command\t\t" + checkcommand['proc:'] + ": " + str(service).split(':')[1] + "\n")

 146     elif serv_tcp.search(service):

 147         f.write("\tservice_description\tTCP port " + str(service).split(':')[1] + "\n")

 148         f.write("\tcheck_command\t\t" + checkcommand['tcp:'] +  str(service).split(':')[1] + "\n")

 149     elif serv_procargs.search(service):

 150         f.write("\tservice_description\tProcess with argument " + str(service).split(':')[1] + "\n")

 151         f.write("\tcheck_command\t\t" + checkcommand['procargs:'] + ": " + str(service).split(':')[1] + "\n")

 152     else:

 153         f.write("\tservice_description\t" + str(service) + "\n")

 154         f.write("\tcheck_command\t\t" + checkcommand[str(service)] + "\n")

 155     f.write("\tuse\t\t\tcritical-service\n")

 156     f.write("\t}\n\n")

 157 

 158 # Profile-related Noncritical services:

 159 for service in services_profile_noncritical[str(my_system['profile'])]:

 160     f.write("define service{\n")

 161     f.write("\thost_name\t\t" + str(my_system['name']) + "\n")

 162     serv_proc = re.compile('proc:')

 163     serv_tcp = re.compile('tcp:')

 164     serv_procargs = re.compile('procargs:')

 165     if serv_proc.search(service):

 166         f.write("\tservice_description\tProcess " + str(service).split(':')[1] + "\n")

 167         f.write("\tcheck_command\t\t" + checkcommand['proc:'] + ": " + str(service).split(':')[1] + "\n")

 168     elif serv_tcp.search(service):

 169         f.write("\tservice_description\tTCP port " + str(service).split(':')[1] + "\n")

 170         f.write("\tcheck_command\t\t" + checkcommand['tcp:'] +  str(service).split(':')[1] + "\n")

 171     elif serv_procargs.search(service):

 172         f.write("\tservice_description\tProcess with argument " + str(service).split(':')[1] + "\n")

 173         f.write("\tcheck_command\t\t" + checkcommand['procargs:'] + ": " + str(service).split(':')[1] + "\n")

 174     else:

 175         f.write("\tservice_description\t" + str(service) + "\n")

 176         f.write("\tcheck_command\t\t" + checkcommand[str(service)] + "\n")

 177     f.write("\tuse\t\t\tnoncritical-service\n")

 178     f.write("\t}\n\n")

 179 

 180 # Standard Critical services:

 181 for service in services_profile_critical['default']:

 182     f.write("define service{\n")

 183     f.write("\thost_name\t\t" + str(my_system['name']) + "\n")

 184     serv_proc = re.compile('proc:')

 185     serv_tcp = re.compile('tcp:')

 186     serv_procargs = re.compile('procargs:')

 187     if serv_proc.search(service):

 188         f.write("\tservice_description\tProcess " + str(service).split(':')[1] + "\n")

 189         f.write("\tcheck_command\t\t" + checkcommand['proc:'] + ": " + str(service).split(':')[1] + "\n")

 190     elif serv_tcp.search(service):

 191         f.write("\tservice_description\tTCP port " + str(service).split(':')[1] + "\n")

 192         f.write("\tcheck_command\t\t" + checkcommand['tcp:'] +  str(service).split(':')[1] + "\n")

 193     elif serv_procargs.search(service):

 194         f.write("\tservice_description\tProcess with argument " + str(service).split(':')[1] + "\n")

 195         f.write("\tcheck_command\t\t" + checkcommand['procargs:'] + ": " + str(service).split(':')[1] + "\n")

 196     else:

 197         f.write("\tservice_description\t" + str(service) + "\n")

 198         f.write("\tcheck_command\t\t" + checkcommand[str(service)] + "\n")

 199     f.write("\tuse\t\t\tcritical-service\n")

 200     f.write("\t}\n\n")

 201 

 202 # Standard Noncritical services:

 203 for service in services_profile_noncritical['default']:

 204     f.write("define service{\n")

 205     f.write("\thost_name\t\t" + str(my_system['name']) + "\n")

 206     serv_proc = re.compile('proc:')

 207     serv_tcp = re.compile('tcp:')

 208     serv_procargs = re.compile('procargs:')

 209     if serv_proc.search(service):

 210         f.write("\tservice_description\tProcess " + str(service).split(':')[1] + "\n")

 211         f.write("\tcheck_command\t\t" + checkcommand['proc:'] + ": " + str(service).split(':')[1] + "\n")

 212     elif serv_tcp.search(service):

 213         f.write("\tservice_description\tTCP port " + str(service).split(':')[1] + "\n")

 214         f.write("\tcheck_command\t\t" + checkcommand['tcp:'] +  str(service).split(':')[1] + "\n")

 215     elif serv_procargs.search(service):

 216         f.write("\tservice_description\tProcess with argument " + str(service).split(':')[1] + "\n")

 217         f.write("\tcheck_command\t\t" + checkcommand['procargs:'] + ": " + str(service).split(':')[1] + "\n")

 218     else:

 219         f.write("\tservice_description\t" + str(service) + "\n")

 220         f.write("\tcheck_command\t\t" + checkcommand[str(service)] + "\n")

 221     f.write("\tuse\t\t\tnoncritical-service\n")

 222     f.write("\t}\n\n")

 223 

 224 # Define all found partitions

 225 for part in partitions:

 226     f.write("define service{\n")

 227     f.write("\thost_name\t\t" + str(my_system['name']) + "\n")

 228     f.write("\tservice_description\tPartition " + str(part) + "\n")

 229     if part == 'swap':

 230         f.write("\tcheck_command\t\tcheck_nrpe!check_swap!10% 5%\n")

 231     else: 

 232         f.write("\tcheck_command\t\tcheck_nrpe!check_disk!10% 5% " + str(part) + "\n")

 233     f.write("\tuse\t\t\tcritical-service\n")

 234     f.write("\t}\n\n")

 235 

 236 f.close()

 237 

 238 # Generate hostgroup file for all .cfg files found in customer's dir

 239 members=[]

 240 dirList=os.listdir(customer_dir)

 241 hostgroupconfig = re.compile('hostgroup.cfg')

 242 

 243 for cfg in dirList:

 244     if not hostgroupconfig.search(cfg):

 245         members.append(cfg.replace('.cfg','') + ', ')

 246 

 247 f=open(customer_dir + '/hostgroup.cfg', 'w')

 248 f.write("define hostgroup{\n")

 249 f.write("\thostgroup_name\t" + options.customer_name + "\n")

 250 f.write("\talias\t\t" + options.customer_name + "'s Systems\n")

 251 f.write("\tmembers\t\t")

 252 for m in members:

 253     f.write(m)

 254 f.write("\n")

 255 f.write("\t}\n")

 256 

 257 f.close()

 258 
